fix(decoders): guard against undefined fields on Bradbury testnet#144
Conversation
Bradbury's consensusDataContract.getTransactionData returns a different struct than studionet/localnet — notably: - numOfInitialValidators is absent; the equivalent field is initialRotations - readStateBlockRange and lastRound sub-fields may be absent Previously decodeTransaction called .toString() unconditionally on all these bigint fields, causing: TypeError: Cannot read properties of undefined (reading 'toString') Fixes: - Use optional chaining + fallback '0' for all bigint field conversions - Fall back to initialRotations when numOfInitialValidators is absent - Update GenLayerRawTransaction type to mark both fields as optional - Guard lastRound sub-fields similarly Tested against: Bradbury tx 0xb3183d4fb2ab7dcafed42e592d7f8cc075d3dfe720c38dd2b45459830326c778
📝 WalkthroughWalkthroughEnhanced transaction decoder safety by implementing optional chaining and nullish coalescing operators across scalar fields and nested properties. Added Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/transactions/decoders.ts (1)
85-86: Remove unnecessaryas anycast.Since
initialRotationsis now properly typed asbigint | undefinedinGenLayerRawTransaction(line 280), theas anycast is no longer needed and bypasses type safety.♻️ Proposed fix
// Bradbury uses `initialRotations`; older chains use `numOfInitialValidators` - numOfInitialValidators: (tx.numOfInitialValidators ?? (tx as any).initialRotations)?.toString() ?? "0", + numOfInitialValidators: (tx.numOfInitialValidators ?? tx.initialRotations)?.toString() ?? "0",🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/transactions/decoders.ts` around lines 85 - 86, The line constructing numOfInitialValidators uses an unnecessary type assertion (tx as any). Remove the cast and rely on the typed property by changing the expression to use tx.numOfInitialValidators ?? tx.initialRotations and then call toString() (or fallback "0"); update the reference in the decoder where numOfInitialValidators is assigned (the expression building numOfInitialValidators from tx) to omit the as any cast and use the properly typed GenLayerRawTransaction.initialRotations instead.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/transactions/decoders.ts`:
- Around line 85-86: The line constructing numOfInitialValidators uses an
unnecessary type assertion (tx as any). Remove the cast and rely on the typed
property by changing the expression to use tx.numOfInitialValidators ??
tx.initialRotations and then call toString() (or fallback "0"); update the
reference in the decoder where numOfInitialValidators is assigned (the
expression building numOfInitialValidators from tx) to omit the as any cast and
use the properly typed GenLayerRawTransaction.initialRotations instead.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: c3d907f3-a6de-4698-99ed-b34a3c8d45a4
📒 Files selected for processing (2)
src/transactions/decoders.tssrc/types/transactions.ts
Problem
decodeTransactioncrashes when called on transactions from Bradbury testnet with:Root cause
Bradbury's
consensusDataContract.getTransactionDatareturns a different struct than studionet/localnet:numOfInitialValidatorsinitialRotationsreadStateBlockRangesub-fieldslastRoundsub-fieldsdecodeTransactioncalled.toString()unconditionally on all thesebigintfields, crashing onundefined.Fix
?? "0"fallback for all bigint field conversionsinitialRotationswhennumOfInitialValidatorsis absentGenLayerRawTransactiontype to mark both fields as optionallastRoundandreadStateBlockRangesub-fields consistentlyTested against
0xb3183d4fb2ab7dcafed42e592d7f8cc075d3dfe720c38dd2b45459830326c7780xd77F1Df3103AfB8715b715992b2DBaf8d5529134npm run build✅Summary by CodeRabbit